import { LogHelper } from "../../utils/logHelper";

export class ReserveControllerTask implements IOuterOreTask{
  run(room: Room, closestRoom: Room): void {
    if(!room)
      return;

    const role = 'outerOre_reserve';
    const isExistRole = this.isExistRole(role,room,closestRoom);
    if(!isExistRole) {
      const controllerLevel = closestRoom.controller?.level;
      if(controllerLevel){
        let body = closestRoom.calculateCreepBody({
          [MOVE]:0.5,
          [CLAIM]:0.5
        });
        if(body){
          closestRoom.spawnQueueAdd(role,body,{ role:role,bornRoom:closestRoom.name ,targetRoom:room.name });
          LogHelper.log(`外矿房间管理[${room.name}] 申请孵化 ${role}`);
        }
        else {
          LogHelper.warn(`外矿房间管理[${room.name}] 房间 ${closestRoom.name} 控制等级 ${controllerLevel} < 3 无法生成 ${role}`);
        }
      }
    }
  }

  private isExistRole(role:string,room: Room, closestRoom: Room):boolean {

    const spawn = closestRoom.spawn();

    if(spawn && spawn.spawning)
      return true;

    const creeps = Object.values(Memory.creeps).filter((c)=> c.targetRoom === room.name && c.role === role);
    const queues = closestRoom.spawnQueueGetRole(role,(c)=> c.memory.targetRoom === room.name)

    if(creeps.length === 0 && queues.length === 0){
      return false;
    }

    return true;
  }
}